﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace MySQLLib
{
    public class Character
    {
        string[,] _character;
        private MySqlConnection conn;

        public int getCharId(string name)
        {
            int id;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "id", "name=" + name);

            Int32.TryParse(result[0], out id);

            return id;
        }

        public string getAccountName(string charname)
        {
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "account", "name=" + charname);

            return result[0];
        }

        public int getType(string name)
        {
            int type;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "type", "name=" + name);

            Int32.TryParse(result[0], out type);

            return type;
        }

        public int getVolume(string name)
        {
            int volume;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "volume", "name=" + name);

            Int32.TryParse(result[0], out volume);

            return volume;
        }

        public int getLevel(string name)
        {
            int level;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "level", "name=" + name);

            Int32.TryParse(result[0], out level);       

            return level;
        }

        public int getStrength(string name)
        {
            int strength;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "strength", "name=" + name);

            Int32.TryParse(result[0], out strength);

            return strength;
        }

        public int getIntellegence(string name)
        {
            int intelligence;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "intelligence", "name=" + name);

            Int32.TryParse(result[0], out intelligence);

            return intelligence;
        }

        public int getAttributePoints(string name)
        {
            int points;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "attribute", "name=" + name);

            Int32.TryParse(result[0], out points);

            return points;
        }

        public Object getHP(string name, bool current = false, bool both = false)
        {
            int hp;
            string[] result;

            if (!current && !both)
            {
                result = Query.SelectQuery(this.conn,"character","hp","name=" + name);
            }
            else if (current && !both)
            {
                result = Query.SelectQuery(this.conn, "character", "s_hp", "name=" + name);
            }
            else if (current && both)
            {
                result = Query.SelectQuery(this.conn, "character", "hp,s_hp", "name=" + name);
                return result;
            }
            else
            {
                Exception ex = new Exception("Couldn't get data. Invaild arguments.");
                throw ex;
            }

            Int32.TryParse(result[0], out hp);

            return hp;
        }

        public Object getMP(string name, bool current = false, bool both = false)
        {
            int mp;
            string[] result;

            if (!current && !both)
            {
                result = Query.SelectQuery(this.conn, "character", "mp", "name=" + name);
            }
            else if (current && !both)
            {
                result = Query.SelectQuery(this.conn, "character", "s_mp", "name=" + name);
            }
            else if (current && both)
            {
                result = Query.SelectQuery(this.conn, "character", "mp,s_mp", "name=" + name);
                return result;
            }
            else
            {
                Exception ex = new Exception("Couldn't get data. Invaild arguments.");
                throw ex;
            }

            Int32.TryParse(result[0], out mp);

            return mp;
        }

        public int getGold(string name)
        {
            int gold;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "gold", "name=" + name);

            Int32.TryParse(result[0], out gold);

            return gold;
        }

        public int getExp(string name)
        {
            int exp;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "experience", "name=" + name);

            Int32.TryParse(result[0], out exp);

            return exp;
        }

        public Object getSP(string name,bool bar = false,bool both = false)
        {
            int sp;
            string[] result;

            if (!bar && !both)
            {
                result = Query.SelectQuery(this.conn, "character", "sp", "name=" + name);
            }
            else if (bar && !both)
            {
                result = Query.SelectQuery(this.conn, "character", "spbar", "name=" + name);
            }
            else if (bar && both)
            {
                result = Query.SelectQuery(this.conn, "character", "sp,spbar", "name=" + name);
                return result;
            }
            else
            {
                Exception ex = new Exception("Couldn't get data. Invaild arguments.");
                throw ex;
            }

            Int32.TryParse(result[0], out sp);

            return sp;
        }

        public bool isGM(string name)
        {
            bool gm = false;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "gm", "name=" + name);

            if (result[0] == "1")
                gm = true;

            return gm;
        }

        public string[] getPos(string name)
        {
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "xpos,ypos,zpos", "name=" + name);

            return result;
        }

        public int[] getSect(string name)
        {
            int[] sec;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "xsect,ysect,zsect", "name=" + name);
            sec = Other.toIntArray(result);

            return sec;
        }

        public Object getPhyAtk(string name, bool min = false, bool both = false)
        {
            int atk;
            string[] result;

            if (!min && !both)
            {
                result = Query.SelectQuery(this.conn, "character", "max_phyatk", "name=" + name);
            }
            else if (min && !both)
            {
                result = Query.SelectQuery(this.conn, "character", "min_phyatk", "name=" + name);
            }
            else if (min && both)
            {
                result = Query.SelectQuery(this.conn, "character", "min_phyatk,max_phyatk", "name=" + name);
                return result;
            }
            else
            {
                Exception ex = new Exception("Couldn't get data. Invaild arguments.");
                throw ex;
            }

            Int32.TryParse(result[0], out atk);

            return atk;
        }

        public Object getMagAtk(string name, bool min = false, bool both = false)
        {
            int atk;
            string[] result;

            if (!min && !both)
            {
                result = Query.SelectQuery(this.conn, "character", "max_magatk", "name=" + name);
            }
            else if (min && !both)
            {
                result = Query.SelectQuery(this.conn, "character", "min_magatk", "name=" + name);
            }
            else if (min && both)
            {
                result = Query.SelectQuery(this.conn, "character", "min_magatk,max_magatk", "name=" + name);
                return result;
            }
            else
            {
                Exception ex = new Exception("Couldn't get data. Invaild arguments.");
                throw ex;
            }

            Int32.TryParse(result[0], out atk);

            return atk;
        }

        public int getPhyDef(string name)
        {
            int def;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "phydef", "name=" + name);
            Int32.TryParse(result[0], out def);
            return def;
        }

        public int getMagDef(string name)
        {
            int def;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "magdef", "name=" + name);
            Int32.TryParse(result[0], out def);
            return def;
        }

        public int getHitRation(string name)
        {
            int hit;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "hit", "name=" + name);
            Int32.TryParse(result[0], out hit);

            return hit;
        }

        public int getParryRation(string name)
        {
            int parry;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "parry", "name=" + name);
            Int32.TryParse(result[0], out parry);

            return parry;
        }

        public int[] getSpeed(string name)
        {
            int[] speed;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "walkspeed,runspeed,berserkspeed", "name=" + name);
            speed = Other.toIntArray(result);

            return speed;
        }

        public int[] getAbsord(string name)
        {
            int[] absord;
            string[] result;

            result = Query.SelectQuery(this.conn,"character","phy_absord,mag_absord","name=" + name);
            absord = Other.toIntArray(result);

            return absord;
        }

        public int getSaveArea(string name)
        {
            int area;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "savearea", "name=" + name);
            Int32.TryParse(result[0], out area);

            return area;
        }

        public int getTitle(string name)
        {
            int title;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "title", "name=" + name);
            Int32.TryParse(result[0], out title);

            return title;
        }

        public string[] getDeleteStatus(string name)
        {
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "deleted,deletedtime", "name=" + name);

            return result;
        }

        public int getInventorySlots(string name)
        {
            int slots;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "slots", "name=" + name);
            Int32.TryParse(result[0], out slots);

            return slots;
        }

        public bool getOnlineStatus(string name)
        {
            bool status = false;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "Online", "name=" + name);

            if (result[0] == "1")
                status = true;

            return status;
        }

        public int getPvpState(string name)
        {
            int state;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "Pvpstate", "name=" + name);
            Int32.TryParse(result[0], out state);

            return state;
        }

        public int getAction(string name)
        {
            int action;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "Action", "name=" + name);
            Int32.TryParse(result[0], out action);

            return action;
        }

        public int[] getPkStatus(string name)
        {
            int[] pk;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "DailyPK,PkLevel,MurderLevel", "name=" + name);
            pk = Other.toIntArray(result);

            return pk;
        }

        public int getGuideData(string name)
        {
            int data;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "GuideData", "name=" + name);
            Int32.TryParse(result[0], out data);

            return data;
        }

        public int getStallModel(string name)
        {
            int model;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "StallModel", "name=" + name);
            Int32.TryParse(result[0], out model);

            return model;
        }

        public bool StorageExpanded(string name)
        {
            bool expanded = false;
            string[] result;

            result = Query.SelectQuery(this.conn, "character", "Storage_Expanded", "name=" + name);
            if (result[0] == "1")
                expanded = true;

            return expanded;
        }

        public void createChar(string account, string name, int chartype, int volume, int level, int strength, int intellegence, int attribute, int hp, int mp, int gold, int experience, int spbar, int sp, int gm, int xsec, int ysec, int xpos, int ypos, int zpos, int s_hp, int s_mp, int min_phyatk, int max_phyatk, int min_magatk, int max_magatk, int phydef, int magdef, int hit, int parry, int walkspeed, int runspeed, int berserkspeed, int mag_absord, int phy_absord, int saverarea, int title, int deleted, string deletedtime, int invslots, int pvpstate, int action, int online, int dailypk, int pklevel, int murderlevel, int guidedata, int stallmodel, int storage_expanden)
        {

        }

        public string[] getCharInfo(string name)
        {
            string[] character;

            character = Query.SelectQuery(this.conn, "character", "*", "name=" + name);

            return character;
        }

        public string[,] getAllChars()
        {

            return this._character;
        }

        public void setConnection(MySqlConnection connection)
        {
            this.conn = connection;
        }

        public void refresh()
        {
            string[] columns = new string[1];
            columns[0] = "*";

            this._character = Query.SelectQuery(this.conn, "character", columns);
        }
    }
}
